<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Query\SettingQuery;
use Illuminate\Http\Request;
use Log;

class SettingController extends Controller
{

    /**
     * 設定マスターSettingMaster
     * 一覧表示
     */
    public function index(Request $request)
    {
       try{
            // 設定マスタ データを取る
            $SettingMasterQuery = new SettingQuery();
            $allSetting = $SettingMasterQuery->getSettingMasterInfo();
            return view('admin/setgoods', compact('allSetting'));
        }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * 設定マスタ保存
     * setting_master
     */
    public function saveSettingMaster(Request $request)
    {
        $SettingMasterQuery = new SettingQuery();

        $param1 = array(
            $request->input('setRate'),
            ' ',
            '税率設定'
        );
        $param2 = array(
            $request->input('setFeed'),
            ' ',
            '送料設定'
        );
        $param3 = array(
            $request->input('setUnpaid'),
            ' ',
            '送料無料設定'
        );
        $param4 = array(
            $request->input('setTime'),
            ' ',
            '当日出荷時刻'
        );
        $param5 = array(
            $request->input('setAbove'),
            ' ',
            '在庫以上'
        );
        $param6 = array(
            $request->input('setStart'),
            $request->input('setEnd'),
            '在庫期間'
        );
        $param7 = array(
            $request->input('setFollowing'),
            ' ',
            '在庫以下'
        );
        $param = [
            $param1,
            $param2,
            $param3,
            $param4,
            $param5,
            $param6,
            $param7
        ];
        // データを保存 setting_master
        $flag = $SettingMasterQuery->saveSettingMaster($param);

        if ($flag) {
            return redirect('/admin/setgoods')->with('success', '追加成功');
        } else {
            return redirect('/admin/setgoods')->with('error', '追加失敗');
        }
    }

    /**
     * 設定マスタ 削除
     * setting_master
     */
    public function deleteSettingMaster(Request $request)
    {
       try{
            $SettingMasterQuery = new SettingQuery();
            $createdAt = $request->input('created_at');

            // 設定マスタ 削除
            $flag = $SettingMasterQuery->deleteSettingMaster($createdAt);

            if ($flag) {
                return redirect('/admin/setgoods')->with('success', '削除成功');
            }
          }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * 設定マスタ 修正
     * setting_master
     */
    public function updateSettingMaster(Request $request)
    {
        try{
                $SettingMasterQuery = new SettingQuery();

                // 設定マスタ 修正
                $SettingMasterInfo = $SettingMasterQuery->selectSettingMasterBycreatedAt();

                return view('admin/settings', compact('SettingMasterInfo'));
        }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * 設定マスタ 修正保存
     * setting_master
     */
    public function saveUpdateSettingMaster(Request $request)
    {
        $createdAt = $request->input('created_at');

        Log::info($createdAt);

        $SettingMasterQuery = new SettingQuery();

        $param1 = array(
            $request->input('setRate'),
            ' ',
            '税率設定',
            $createdAt
        );
        $param2 = array(
            $request->input('setFeed'),
            ' ',
            '送料設定',
            $createdAt
        );
        $param3 = array(
            $request->input('setUnpaid'),
            ' ',
            '送料無料設定',
            $createdAt
        );
        $param4 = array(
            $request->input('setTime'),
            ' ',
            '当日出荷時刻',
            $createdAt
        );
        $param5 = array(
            $request->input('setAbove'),
            ' ',
            '在庫以上',
            $createdAt
        );
        $param6 = array(
            $request->input('setStart'),
            $request->input('setEnd'),
            '在庫期間',
            $createdAt
        );
        $param7 = array(
            $request->input('setFollowing'),
            ' ',
            '在庫以下',
            $createdAt
        );
        $param = [
            $param1,
            $param2,
            $param3,
            $param4,
            $param5,
            $param6,
            $param7
        ];

        // データを修正 setting_master
        $flag = $SettingMasterQuery->updateSettingMaster($param);

        return redirect('/admin/home')->with('success', '追加成功');
    }
}